<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="设施名称" prop="facilityNo">
                <el-select class="w100p" v-model="ruleForm.facilityNo" clearable filterable placeholder="请选择设施名称">
                    <el-option v-for="(item, index) in facilityList" :key="index" :label="item.facilityName"
                        :value="item.facilityNo" />
                </el-select>
            </el-form-item>
            <el-form-item label="数据类型" prop="dataTypeConfigId">
                <el-select class="w100p" v-model="ruleForm.dataTypeConfigId" clearable filterable placeholder="请选择物联数据项">
                    <el-option v-for="(item, index) in itemList" :key="index" :label="item.dataTypeName"
                        :value="item.dataTypeConfigId" />
                </el-select>
            </el-form-item>
            <el-form-item prop="value" label="具体数据">
                <el-input type="number" class="w100p" v-model="ruleForm.value" placeholder="请输入具体数据"></el-input>
            </el-form-item>
            <el-form-item label="物联单位" prop="unitNo">
                <el-select class="w100p" v-model="ruleForm.unitNo" clearable filterable placeholder="请选择物联单位">
                    <el-option v-for="(item, index) in unitList" :key="index" :label="item.unitName" :value="item.unitNo" />
                </el-select>
            </el-form-item>
            <el-form-item label="录入人:" prop="account">
                <el-select class="w100p" v-model="ruleForm.account" value-key="personNo" filterable clearable
                    placeholder="请选择录入人">
                    <el-option v-for="(item, index) in personList" :key="index" :label="item.person.name"
                        :value="item.person.name" />
                </el-select>
            </el-form-item>
            <el-form-item label="录入日期:" prop="collectDatetime">
                <el-date-picker class="w100p" v-model="ruleForm.collectDatetime" format="yyyy-MM-dd HH:mm:ss"
                    value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择投产时间" filterable />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import environmentalDataEntry from '@/api/iotMonitoring/environmentalDataEntry.js';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        varietyList: {
            type: Array,
            default: () => {
                return []
            }
        },
        unitList: {
            type: Array,
            default: () => {
                return []
            }
        },
        itemList: {
            type: Array,
            default: () => {
                return []
            }
        },
        facilityList: {
            type: Array,
            default: () => {
                return []
            }
        },
        personList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value <= 999999) {
                callback()
            } else {
                callback(new Error('请输入大于0小于999999的数值！'))
            }
        }
        return {
            addEditName: '添加',
            addEditApi: environmentalDataEntry.add,
            ruleForm: {},
            rules: {
                facilityNo: [
                    { required: true, message: '请选择设施', trigger: 'change' }
                ],
                dataTypeConfigId: [
                    { required: true, message: '请选择数据类型', trigger: 'change' }
                ],
                value: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                unitNo: [
                    { required: true, message: '请选择单位', trigger: 'change' }
                ],
                account: [
                    { required: true, message: '请选择录入人', trigger: 'change' }
                ],
                collectDatetime: [
                    { required: true, message: '请选择录入时间', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = environmentalDataEntry.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = environmentalDataEntry.edit
                this.addEditName = '编辑'
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.itemList.forEach((el) => {
                        if (el.dataTypeConfigId === this.ruleForm.dataTypeConfigId) {
                            this.ruleForm["dataTypeName"] = el.dataTypeName;
                        }
                    });
                    this.facilityList.forEach((el) => {
                        if (el.facilityNo === this.ruleForm.facilityNo) {
                            this.ruleForm["facilityName"] = el.facilityName;
                        }
                    });
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  